package torres.map;

import torres.fileIO.ClassContainer;
import torres.fileIO.Saveable;
import torres.map.areaeffect.AreaEffect;
import torres.map.areaeffect.HealDamage;
import torres.map.areaeffect.InstantDeath;
import torres.map.areaeffect.LevelUp;
import torres.map.areaeffect.TakeDamage;
import torres.map.areaeffect.Teleport;
import torres.map.decal.Biohazard;
import torres.map.decal.Decal;
import torres.map.decal.GoldStar;
import torres.map.decal.Portal;
import torres.map.decal.RedCross;
import torres.map.decal.SkullCrossbones;
import torres.map.terrain.DojoFloor;
import torres.map.terrain.DojoWall;
import torres.map.terrain.Grass;
import torres.map.terrain.Mountain;
import torres.map.terrain.Terrain;
import torres.map.terrain.Water;

/**
 * @author Torres
 * Represents a location on a Map object which contains an AreaEffect,
 * Decal, and Terrain.
 */
public class Tile implements Saveable{
	
	private AreaEffect effect;
	private Decal decal;
	private Terrain terrain;
	
	/**
	 * Constructs a new Tile object
	 */
	public Tile() {
		
		this.effect = null;
		this.decal = null;
		this.terrain = null;
	}
	
	/**
	 * @return the area effect associated with this tile
	 */
	public AreaEffect getAreaEffect() {
		
		return effect;
	}
	
	/**
	 * @return the decal associated with this tile
	 */
	public Decal getDecal() {
		
		return decal;
	}
	
	/**
	 * @return the terrain associated with this tile
	 */
	public Terrain getTerrain() {
		
		return terrain;
	}
	
	/**
	 * @param effect the area effect to be applied to this tile
	 */
	public void setAreaEffect(AreaEffect effect) {
		
		this.effect = effect;
	}
	
	/**
	 * @param decal the decal to be applied to this tile
	 */
	public void setDecal(Decal decal) {
		
		this.decal = decal;
	}
	
	/**
	 * @param terrain the terrain to be applied to this tile
	 */
	public void setTerrain(Terrain terrain) {
		
		this.terrain = terrain;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		
		if(terrain != null) {
			
			if(decal != null) {
				
				return (decal.toString() + terrain.toString() + decal.toString());
			}
			else {
				
				return (" " + terrain.toString() + " ");
			}
		}
		else {
			
			return "   ";
		}
	}

	/* (non-Javadoc)
	 * @see torres.fileIO.Saveable#save()
	 */
	@Override
	public ClassContainer save() {
		ClassContainer curClass = new ClassContainer("Tile");
		
		if(effect!=null) curClass.addProperty(effect.save());
		if(decal!=null) curClass.addProperty(decal.save());
		if(terrain!=null) curClass.addProperty(terrain.save());
		
		return curClass;
	}

	/* (non-Javadoc)
	 * @see torres.fileIO.Saveable#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer classContainer) {
		ClassContainer effectCont;
		if((effectCont=(ClassContainer)classContainer.getProperty("HealDamage"))!=null)
			effect = new HealDamage();
		else if((effectCont=(ClassContainer)classContainer.getProperty("InstantDeath"))!=null)
			effect = new InstantDeath();
		else if((effectCont=(ClassContainer)classContainer.getProperty("LevelUp"))!=null)
			effect = new LevelUp();
		else if((effectCont=(ClassContainer)classContainer.getProperty("TakeDamage"))!=null)
			effect = new TakeDamage();
		else if((effectCont=(ClassContainer)classContainer.getProperty("Teleport"))!=null)
			effect = new Teleport();
		if(effectCont!=null)effect.load(effectCont);
		@SuppressWarnings("unused")
		ClassContainer decalCont = (ClassContainer)classContainer.getProperty("Decal");
		if((decalCont=(ClassContainer)classContainer.getProperty("GoldStar"))!=null)
			decal = GoldStar.getInstance();
		else if((decalCont=(ClassContainer)classContainer.getProperty("Biohazard"))!=null)
			decal = Biohazard.getInstance();
		else if((decalCont=(ClassContainer)classContainer.getProperty("Portal"))!=null)
			decal = Portal.getInstance();
		else if((decalCont=(ClassContainer)classContainer.getProperty("RedCross"))!=null)
			decal = RedCross.getInstance();
		else if((decalCont=(ClassContainer)classContainer.getProperty("SkullCrossbones"))!=null)
			decal = SkullCrossbones.getInstance();
		@SuppressWarnings("unused")
		ClassContainer terrainCont = (ClassContainer)classContainer.getProperty("Terrain");
		if((terrainCont=(ClassContainer)classContainer.getProperty("Grass"))!=null)
			terrain = Grass.getInstance();
		else if((terrainCont=(ClassContainer)classContainer.getProperty("Mountain"))!=null)
			terrain = Mountain.getInstance();
		else if((terrainCont=(ClassContainer)classContainer.getProperty("Water"))!=null)
			terrain = Water.getInstance();
		else if((terrainCont=(ClassContainer)classContainer.getProperty("DojoFloor"))!=null)
			terrain = DojoFloor.getInstance();
		else if((terrainCont=(ClassContainer)classContainer.getProperty("DojoWall"))!=null)
			terrain = DojoWall.getInstance();
	}
}
